package com.borqsprocessmanager.view.progressbar;

import java.text.NumberFormat;

import com.borqsprocessmanager.util.Log;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.ArcShape;
import android.graphics.drawable.shapes.Shape;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

public class MyProgressBar extends View {
	private final static String TAG = "MyProgressBar";

	public ShapeDrawable[] mDrawables;
	private float startSweepAngle = 360f;
	private float mProgressSpeed = 2.5f;
	private float mProgessEnd = 150;

	private MyProgressBar instance;
	private boolean decelerate = false;

	private long time = 15;
	MyHandler handler = new MyHandler();

	private int mWidth, mHeight;

	// private static Shader makeSweep() {
	// return new SweepGradient(0, 180, new int[] { 0xFFFF0000, 0xFF00FF00,
	// 0xFF0000FF, 0xFFFF0000 }, null);
	// }

	private static Shader makeSweep() {
		return new SweepGradient(0, 0,
				new int[] { 0xFFE900FF, 0xFF4600FF, 0, 0 }, null);
	}

	private static Shader makeLinear() {
		return new LinearGradient(0, 0, 50, 50, new int[] { 0xFFFF0000,
				0xFF00FF00, 0xFF0000FF }, null, Shader.TileMode.REPEAT);
	}

	private static Shader makeTiling() {
		int[] pixels = new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0 };
		Bitmap bm = Bitmap.createBitmap(pixels, 2, 2, Bitmap.Config.ARGB_8888);

		return new BitmapShader(bm, Shader.TileMode.REPEAT,
				Shader.TileMode.REPEAT);
	}

	private static class MyShapeDrawable extends ShapeDrawable {
		private Paint mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

		public MyShapeDrawable(Shape s) {
			super(s);
			mStrokePaint.setStyle(Paint.Style.STROKE);
		}

		public Paint getStrokePaint() {
			return mStrokePaint;
		}

		@Override
		protected void onDraw(Shape s, Canvas c, Paint p) {
			s.draw(c, p);
			s.draw(c, mStrokePaint);
		}
	}

	public MyProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public MyProgressBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public MyProgressBar(Context context) {
		super(context);
		init();
	}

	private void init() {
		// setFocusable(true);
		ShapeDrawable s;
		// float[] outerR = new float[] { 12, 12, 12, 12, 12, 12, 12, 12 };
		// RectF inset = new RectF(6, 6, 6, 6);
		// float[] innerR = new float[] { 12, 12, 0, 0, 12, 12, 0, 0 };

		// Path path = new Path();
		// path.moveTo(50, 0);
		// path.lineTo(0, 50);
		// path.lineTo(50, 100);
		// path.lineTo(100, 50);
		// path.close();

		mDrawables = new ShapeDrawable[7];
		// mDrawables[0] = new ShapeDrawable(new RectShape());
		// mDrawables[1] = new ShapeDrawable(new OvalShape());
		// mDrawables[2] = new ShapeDrawable(new RoundRectShape(outerR,
		// null,
		// null));
		// mDrawables[3] = new ShapeDrawable(new RoundRectShape(outerR,
		// inset,
		// null));
		// mDrawables[4] = new ShapeDrawable(new OvalShape());
		// mDrawables[5] = new ShapeDrawable(new PathShape(path, 100, 100));
		mDrawables[6] = new MyShapeDrawable(new ArcShape(-90, 360));
		//
		// mDrawables[0].getPaint().setColor(0xFFFF0000);
		// mDrawables[1].getPaint().setColor(0xFF00FF00);
		// mDrawables[2].getPaint().setColor(0xFF0000FF);
		// mDrawables[3].getPaint().setShader(makeSweep());
		// mDrawables[4].getPaint().setShader(makeLinear());
		// mDrawables[5].getPaint().setShader(makeTiling());
		mDrawables[6].getPaint().setShader(makeSweep());

		// PathEffect pe = new DiscretePathEffect(10, 4);
		// PathEffect pe2 = new CornerPathEffect(4);
		// mDrawables[3].getPaint().setPathEffect(
		// new ComposePathEffect(pe2, pe));

		// MyShapeDrawable msd = (MyShapeDrawable) mDrawables[6];
		// msd.getStrokePaint().setStrokeWidth(4);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		Log.v(TAG, "widthMeasureSpec===" + widthMeasureSpec);
		Log.v(TAG, "heightMeasureSpec===" + heightMeasureSpec);
		mWidth = getMeasuredWidth();
		mHeight = getMeasuredHeight();

	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.v(TAG, "onDraw");
		mDrawables[6].setBounds(0, 0, mWidth, mWidth);
		mDrawables[6].draw(canvas);
		// for (Drawable dr : mDrawables) {
		// dr.setBounds(x, y, x + width, y + height);
		// dr.draw(canvas);
		// y += height + 5;
		// }
	}

	public void startAnimaction() {
		// new AnimactionThread(mDrawables).start();

		// MyHandler handler = new MyHandler();
		// while (startSweepAngle >= 160) {
		if (mProgessEnd > 315) {
			mProgressSpeed = 1;
			decelerate = false;
		} else {
			decelerate = true;
		}
		handler.sendEmptyMessageDelayed(0, 500);
		// }
	}

	class MyHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			// drawables[6] = new MyShapeDrawable(new ArcShape(-90,
			// startSweepAngle));
			mDrawables[6].setShape(new ArcShape(-90, startSweepAngle));
			invalidate();
			startSweepAngle -= mProgressSpeed;

			if (decelerate && startSweepAngle < mProgessEnd + 45
					&& mProgressSpeed > 0.1) {
				mProgressSpeed -= 0.07;
			}

			// time = getInterpolation(time);

			if (startSweepAngle >= mProgessEnd) {
				handler.sendEmptyMessageDelayed(0, 16);
			}
		}

	}

	// public float getInterpolation(float input) {
	// float result;
	// if (mFactor == 1.0f) {
	// result = (float) (1.0f - (1.0f - input) * (1.0f - input));
	// } else {
	// result = (float) (1.0f - Math.pow((1.0f - input), 2 * mFactor));
	// }
	// return result;
	// }

	class AnimactionThread extends Thread {
		ShapeDrawable[] drawables;

		AnimactionThread(ShapeDrawable[] mDrawables) {
			drawables = mDrawables;
		}

		@Override
		public void run() {
			int startSweepAngle = 360;
			while (startSweepAngle >= 160) {

				postInvalidate();
				try {
					sleep(17);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void setmProgessEnd(int level) {
		NumberFormat numberFormat = NumberFormat.getInstance();// 设置精确到小数点后2位
		numberFormat.setMaximumFractionDigits(2);
		String result = numberFormat.format((float) level / (float) 100 * 100);
		Log.v(TAG, result + "===========level");
		float levelPercent = (((360) * level) / 100);
		Log.v(TAG, levelPercent + "===========levelPercent");
		this.mProgessEnd = levelPercent;
	}
}
